package com.substation.ui.activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.annotation.IdRes;
import android.support.v7.widget.Toolbar;
import android.widget.RadioGroup;

import com.substation.R;
import com.substation.ui.fragment.EqptFragment;
import com.substation.ui.fragment.StandbyFragment;
import com.substation.ui.fragment.ToolsFragment;
import com.substation.utils.SPUtils;

import butterknife.BindView;

public class SubstationDetailActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    int select = 0;
    Fragment eqptFragment, standbyFragment, toolsFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_substation_detail;
    }

    @Override
    protected void initView() {
        radioGroup.check(R.id.radioButton1);
        radioGroup.setOnCheckedChangeListener(this);
        selecte(select);
    }

    @Override
    protected void initData() {
        toolbar.setTitle((String) SPUtils.get(context, "sub_name", ""));
    }

    /**
     * 设置fragment显示以及切换
     *
     * @param i
     */
    private void selecte(int i) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction, i);
        switch (i) {
            case 0:
                if (eqptFragment == null) {
                    eqptFragment = new EqptFragment();
                    fragmentTransaction.add(R.id.content, eqptFragment);
                } else {
                    fragmentTransaction.show(eqptFragment);
                }
                break;
            case 1:
                if (standbyFragment == null) {
                    standbyFragment = new StandbyFragment();
                    fragmentTransaction.add(R.id.content, standbyFragment);
                } else {
                    fragmentTransaction.show(standbyFragment);
                }
                break;
            case 2:
                if (toolsFragment == null) {
                    toolsFragment = new ToolsFragment();
                    fragmentTransaction.add(R.id.content, toolsFragment);
                } else {
                    fragmentTransaction.show(toolsFragment);
                }
                break;
        }
        fragmentTransaction.commit();
    }

    private void hideFragment(FragmentTransaction ft, int i) {
        if (eqptFragment != null && i != 0)
            ft.hide(eqptFragment);
        if (standbyFragment != null && i != 1)
            ft.hide(standbyFragment);
        if (toolsFragment != null && i != 2)
            ft.hide(toolsFragment);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
        switch (i) {
            case R.id.radioButton1:
                select = 0;
                break;
            case R.id.radioButton2:
                select = 1;
                break;
            case R.id.radioButton3:
                select = 2;
                break;
        }
        selecte(select);
    }
}
